home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-10
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: gets() question
- Date: 8 Jan 1996 08:33:44 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4cqkt8$1quo@news.gate.net>
- References: <4cosgf$rir@newsbf02.news.aol.com>
- NNTP-Posting-Host: pslfl2-10.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4cosgf$rir@newsbf02.news.aol.com>,
- simpsondg@aol.com (SimpsonDG) wrote:
- >Can any of you C gurus out there help me with this? I have a problem with
- >gets() that I don't understand. The program below will successfully input
- >the string s[0], but will simply ignore the gets() that inputs s[1] and
- >goes on to ask for i[1]. What's the deal? A little experimenting seems
- >to indicate that the problem arises when I have a gets() following a
- >scanf() -- e.g., a program using only gets() or only scanf() works fine.
- >
- >David Simpson
- >
- >--------------------------------------------------------------------------
- >-------------------
- >
- >#include "stdio.h"
- >
- >void main(void)
- >{
- > int i[5];
- > char s[3][10];
- >
- > printf ("Enter s[0]: ");
- > gets (s[0]);
- ^^^^
-
- Your compiler didn't generate an error? *s* is not an array of pointers to
- arrays of chars as in argv[]. Now I remember why I always like being explicit.
- Try this:
-
- fgets(&s[0][0],9,stdin);
-
- You can use a 2 dimensional char array, except that you have to be complete
- in your form and s[0] is not. Because fgets() (or gets() for that matter) is
- looking for an address, you need to oblige, hence the prefix of *&*. The
- reason for fgets() is to prevent overflowing your array by including a limit.
- I strongly suggest you read the FAQ. You have several situations (including
- the use of the dreaded scanf() and void main()) that need thorough
- understanding. I think it's important that people understand that the FAQ for
- comp.lang.c is not just "answers to frequently asked questions," as it might
- be in other newsgroups, but an important "fill in the cracks" text that
- belongs with the best of your C references. As a matter of fact, get the book
- when you can.
-
- Bill
-
- "Whatcha got on?...Your mind?"
-